<template>
  <div>
    <div class="box4">
      <el-button type="success" @click="flag = true">添加用户</el-button>
      <el-select v-model="id" placeholder="请选择">
        <el-option
          v-for="item in list1"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-input placeholder="请输入内容" v-model="keyword"></el-input>
      <el-button type="primary" @click="getlist">搜索</el-button>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="用户" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" />
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="会员等级" width="180">
        <template slot-scope="scope">
          {{ scope.row.user_level.name }}
        </template>
      </el-table-column>
      <el-table-column prop="update_time" label="登录时间"> </el-table-column>
      <el-table-column label="状态">
        <el-button plain v-if="(status = 1)" disabled>禁用</el-button>
        <el-button plain v-if="(status = 0)">启用</el-button>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="del(scope.row)">删除</el-button>
          <el-button>编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="changesize"
      @current-change="changepage"
      :current-page="page"
      :page-sizes="[3, 6, 9, 12]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="zong"
    >
    </el-pagination>
    <!-- 添加用户 -->
    <el-dialog title="添加用户" :visible.sync="flag">
      <el-form :model="form">
        <el-form-item label="用户名称">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <img
            :src="form. avatar"
            alt=""
          />
        </el-form-item>
        <el-form-item label="会员等级">
          <el-select v-model="form.user_level_id" placeholder="请选择">
            <el-option
              v-for="item in list1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="radio1" label="1" border>启用</el-radio>
          <el-radio v-model="radio1" label="2" border>禁用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="tj">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      limit: 3,
      keyword: "",
      zong: 0,
      list1: [],
      id: "",
      //添加用户弹框
      flag: false,
      //用户信息
      form: {
        username: "",
        password: "",
        nickname: "",
        avatar: "http://tangzhe123-com.oss-cn-shenzhen.aliyuncs.com/public/5fe28b4f7feba.jpg",
        user_level_id: "",
        phone: "",
        email: "",
        status: "",
      },
      radio1: 1,
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    getlist() {
      this.$http
        .get(
          `/admin/user/${this.page}?limit=${this.limit}&keyword=${this.keyword}&user_level_id=${this.id}`
        )
        .then((res) => {
          console.log(res);
          this.list = res.data.data.list;
          this.zong = res.data.data.totalCount;
          this.list1 = res.data.data.user_level;
        });
    },
    //
    changepage(page) {
      this.page = page;
      this.getlist();
    },
    changesize(size) {
      this.limit = size;
      this.getlist();
    },
    //删除
    del(item) {
      this.$http
        .post(`/admin/user/${item.id}/delete`)
        .then((res) => {
          console.log(res);
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getlist();
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    //添加
    tj() {
      this.flag = false;
      this.$http
        .post("admin/user", this.form)
        .then((res) => {
          console.log(res);
          this.getlist()
          console.log(this.list)
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
  },
};
</script>

<style lang='scss' scoped>
.box4 {
  width: 100%;
  display: flex;
  margin-top: 10px;
  margin-bottom: 10px;
}
img {
  width: 60px;
  height: 60px;
}
.el-form-item__content {
  display: flex;
}
</style>